<?
require_once(dirname(__FILE__).'/../models/Countries.php');

class CountriesController extends Main_Controller_Action {	
	public function preDispatch()
    {
        if (!unserialize(Zend_Auth::getInstance()->getIdentity())->getIsAdministrator()) {        
                $this->_helper->redirector('', '');
        } else {
           /**
			 * Translation - Loading dictionaries common and backoffice_common
			 */
			$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
			$this->view->commonLocale = $commonLocale;
        }
    }
    
	function indexAction(){
		try{
				$countries = ModelMapper::instance('Countries')->findAll();
		} catch (DatabaseException $e){
				$e->getMessage();
		}
			
		$this->view->countries = $countries;
	}
	
	function addAction(){
		
		$smarty = $this->view->getEngine();
		$tpl = $smarty->fetch('countries/add.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function editAction(){
		$obj = ModelMapper::instance('Countries')->find(intval($this->getRequest()->getParam('countriesId')));
		
		$smarty = $this->view->getEngine();
		
		$this->view->country = $obj;
		$tpl = $smarty->fetch('countries/edit.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function saveAction(){
		//Database::instance()->getConn()->debug = 1;
		$formData = $this->_request->getParam('addForm');
		
		//debug($formData);

		if (intval($formData['countriesId']) >0) {
			$obj = ModelMapper::instance('Countries')->find(intval($formData['countriesId']));
		}
		else {
			$obj = new Countries();
		}
		
		$obj->setCountry(trim(strip_tags(stripcslashes($formData['country']))));
		
		exit();
	}
	
	function deleteAction(){
		$obj = ModelMapper::instance('Countries')->find(intval($this->getRequest()->getParam('countriesId')));
		$obj->delete();
		
		exit();
	}
	
	
}

	
?>
